<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="identifier">
        <el-input
          v-model="queryParams.identifier"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="mnemonicCode">
        <el-input
          v-model="queryParams.mnemonicCode"
          placeholder="请输入助记码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架状态" prop="marketEnable">
        <el-select v-model="queryParams.marketEnable" placeholder="上架状态" clearable>
          <el-option v-for="dict in dict.type.goods_market_enable" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品类型" prop="marketEnable">
        <el-select v-model="queryParams.goodsType" placeholder="商品类型" clearable>
          <el-option v-for="dict in dict.type.goods_sku_type" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存状态" prop="haveQuantity">
        <el-select v-model="queryParams.paramMap.haveQuantity" placeholder="库存状态" clearable>
          <el-option label="有" :value="true" />
          <el-option label="无" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- :disabled="!synchronizeOrders" -->
        <el-button type="success" plain icon="el-icon-grape" size="mini"
          @click="handleSyncQuantity" v-hasPermi="['offline:goods:sku:sync:erp']">同步商品</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- :disabled="!synchronizeOrders" -->
        <el-button type="success" plain icon="el-icon-grape" size="mini"
          @click="handleSyncQuantityQuantity" v-hasPermi="['offline:goods:sku:quantity:sync:erp']">同步库存</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- :disabled="!synchronizeOrders" -->
        <el-button type="success" plain icon="el-icon-grape" size="mini"
          @click="handleSyncHasQuantity" v-hasPermi="['offline:goods:sku:hasQuantity:erp']">刷新无库存</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column :label="columns[0].label" align="left" :prop="columns[0].prop"
        :width="columns[0].width" :show-overflow-tooltip="true" v-if="columns[0].visible" />
      <el-table-column :label="columns[1].label" align="left" :prop="columns[1].prop"
        :width="columns[1].width" :show-overflow-tooltip="true" v-if="columns[1].visible" />
      <el-table-column :label="columns[2].label" align="left" :prop="columns[2].prop"
        :width="columns[2].width" :show-overflow-tooltip="true" v-if="columns[2].visible" />
      <el-table-column :label="columns[3].label" align="left" :prop="columns[3].prop"
        :width="columns[3].width" :show-overflow-tooltip="true" v-if="columns[3].visible" />
      <el-table-column :label="columns[4].label" align="left" :prop="columns[4].prop"
        :width="columns[4].width" :show-overflow-tooltip="true" v-if="columns[4].visible" />
      <el-table-column :label="columns[5].label" align="left" :prop="columns[5].prop"
        :width="columns[5].width" :show-overflow-tooltip="true" v-if="columns[5].visible" />
      <el-table-column :label="columns[6].label" align="left" :prop="columns[6].prop"
        :width="columns[6].width" :show-overflow-tooltip="true" v-if="columns[6].visible" />
      <el-table-column :label="columns[7].label" align="left" :prop="columns[7].prop"
        :width="columns[7].width" :show-overflow-tooltip="true" v-if="columns[7].visible" />
      <el-table-column :label="columns[8].label" align="left" :prop="columns[8].prop"
        :width="columns[8].width" :show-overflow-tooltip="true" v-if="columns[8].visible" />
      <el-table-column :label="columns[9].label" align="left" :prop="columns[9].prop"
        :width="columns[9].width" :show-overflow-tooltip="true" v-if="columns[9].visible" />
      <el-table-column :label="columns[10].label" align="center" :width="columns[10].width"
        :show-overflow-tooltip="true" v-if="columns[10].visible">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.marketEnable" active-value="UPPER" inactive-value="DOWN"
            @change="handleMarketEnableStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column :label="columns[11].label" align="left" :prop="columns[11].prop"
        :width="columns[11].width" :show-overflow-tooltip="true" v-if="columns[11].visible" />
      <el-table-column :label="columns[12].label" align="left" :prop="columns[12].prop"
        :width="columns[12].width" :show-overflow-tooltip="true" v-if="columns[12].visible" />
      <el-table-column :label="columns[13].label" align="left" :prop="columns[13].prop"
        :width="columns[13].width" :show-overflow-tooltip="true" v-if="columns[13].visible" />
      <el-table-column :label="columns[14].label" align="left" :prop="columns[14].prop"
        :width="columns[14].width" :show-overflow-tooltip="true" v-if="columns[14].visible" />
      <el-table-column :label="columns[15].label" align="left" :prop="columns[15].prop"
        :width="columns[15].width" :show-overflow-tooltip="true" v-if="columns[15].visible" />
      <el-table-column :label="columns[16].label" align="left" :prop="columns[16].prop"
        :width="columns[16].width" :show-overflow-tooltip="true" v-if="columns[16].visible" />
      <el-table-column :label="columns[17].label" align="left" :prop="columns[17].prop"
        :width="columns[17].width" :show-overflow-tooltip="true" v-if="columns[17].visible" />
      <el-table-column :label="columns[18].label" align="left" :prop="columns[18].prop"
        :width="columns[18].width" :show-overflow-tooltip="true" v-if="columns[18].visible" />
      <el-table-column :label="columns[19].label" align="left" :prop="columns[19].prop"
        :width="columns[19].width" :show-overflow-tooltip="true" v-if="columns[19].visible" />
      <el-table-column :label="columns[20].label" align="left" :prop="columns[20].prop"
        :width="columns[20].width" :show-overflow-tooltip="true" v-if="columns[20].visible" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_sku_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column :label="columns[21].label" align="left" :prop="columns[21].prop"
        :width="columns[21].width" :show-overflow-tooltip="true" v-if="columns[21].visible" />
      <el-table-column :label="columns[22].label" align="left" :prop="columns[22].prop"
        :width="columns[22].width" :show-overflow-tooltip="true" v-if="columns[22].visible" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listGoodsSku, changeGoodsSkuMarketEnable } from "@/api/goods/offlineGoods";

export default {
  name: "Post",
  dicts: ['goods_market_enable', 'goods_sku_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        identifier: undefined,
        marketEnable: undefined,
        goodsType: undefined,
        paramMap: {
          haveQuantity: undefined,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      },
      // 列信息
      columns: [
        { key: 0, label: `商品ID`, prop: "id", width: 180, visible: false },
        { key: 1, label: `商品名称`, prop: "goodsName", width: 200, visible: true },
        { key: 2, label: `经营范围`, prop: "classification", width: 100, visible: false },
        { key: 3, label: `最低售价`, prop: "price", width: 80, visible: true },
        { key: 4, label: `成本价`, prop: "cost", width: 80, visible: true },
        { key: 5, label: `最低含税售价`, prop: "taxPrice", width: 100, visible: true },
        { key: 6, label: `商品编码`, prop: "identifier", width: 80, visible: true },
        { key: 7, label: `库存`, prop: "quantity", width: 70, visible: true },
        { key: 8, label: `计量单位`, prop: "goodsUnit", width: 80, visible: false },
        { key: 9, label: `规格`, prop: "simpleSpecs", width: 100, visible: true },
        { key: 10, label: `上架状态`, prop: "marketEnable", width: 80, visible: true },
        { key: 11, label: `条码`, prop: "barCode", width: 100, visible: false },
        { key: 12, label: `效期`, prop: "expirationDate", width: 100, visible: true },
        { key: 13, label: `商品分类`, prop: "assort", width: 80, visible: true },
        { key: 14, label: `生产厂家`, prop: "manufacturer", width: 180, visible: true },
        { key: 15, label: `产地`, prop: "place", width: 100, visible: false },
        { key: 16, label: `买赠信息`, prop: "giftMsg", width: 180, visible: true },
        { key: 17, label: `折让信息`, prop: "discountMsg", width: 180, visible: true },
        { key: 18, label: `中包装`, prop: "pack", width: 60, visible: true },
        { key: 19, label: `件装量`, prop: "crate", width: 60, visible: true },
        { key: 20, label: `商品类型`, prop: "goodsType", width: 80, visible: false },
        { key: 21, label: `助记码`, prop: "mnemonicCode", width: 120, visible: false },
        { key: 22, label: `创建时间`, prop: "createTime", width: 160, visible: false },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoodsSku(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        identifier: undefined,
        marketEnable: undefined,
        goodsType: undefined,
        paramMap: {
          haveQuantity: undefined,
        },
      };

      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 上架状态修改
    handleMarketEnableStatusChange (row) {
      let text = row.marketEnable === "UPPER" ? "上架" : "下架";
      this.$modal.confirm('确认"' + text + '""' + row.goodsName + '"商品吗？')
        .then(function () {
          return changeGoodsSkuMarketEnable(
            row.id,
            row.marketEnable
          );
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.marketEnable = row.marketEnable === "UPPER" ? "DOWN" : "UPPER";
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
    handleSyncQuantity(){
      window.open('http://www.xdyy365.com:5003/wholesale/sync-api/offline/increment/sync/goods/sku', '_black')
    },
    handleSyncQuantityQuantity(){
      window.open('http://www.xdyy365.com:5003/wholesale/sync-api/offline/increment/sync/goods/sku', '_black')
    },
    handleSyncHasQuantity(){
      window.open('http://www.xdyy365.com:5003/wholesale/sync-api/offline/all/stock/sync', '_black')
    },
  }
};
</script>
